﻿using HandyControl.Controls;
using HandyControl.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using MessageBox = HandyControl.Controls.MessageBox;
using Window = System.Windows.Window;

namespace FieldTools.Client.View.Window
{
    /// <summary>
    /// BaseControlWindow.xaml 的交互逻辑
    /// </summary>
    public partial class BaseControlWindow : System.Windows.Window
    {
        public BaseControlWindow()
        {
            InitializeComponent();
        }


        /// <summary>
        /// 自定义控件标题
        /// </summary>
        public string? MyTitle
        {
            get { return (string)GetValue(MyTitleProperty); }
            set { SetValue(MyTitleProperty, value); }
        }

        // Using a DependencyProperty as the backing store for MyTitle.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MyTitleProperty =
            DependencyProperty.Register("MyTitle", typeof(string), typeof(BaseControlWindow), null);



        /// <summary>
        /// 自定义控件内容
        /// </summary>
        public UserControl MyUserControl
        {
            get { return (UserControl)GetValue(MyUserControlProperty); }
            set { SetValue(MyUserControlProperty, value); }
        }

        // Using a DependencyProperty as the backing store for MyUserControl.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MyUserControlProperty =
            DependencyProperty.Register("MyUserControl", typeof(UserControl), typeof(BaseControlWindow), null);




        /// <summary>
        /// 设置窗口置顶
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SetTopmost(object sender, RoutedEventArgs e)
        {
            Topmost = !Topmost;
            if (Topmost == true)
            {
                Growl.Success(new()
                {
                    Message = "窗口已置顶",
                    Type = InfoType.Success,
                    WaitTime = 1
                });
            }
            else
            {
                Growl.Success(new()
                {
                    Message = "窗口已取消置顶",
                    Type = InfoType.Success,
                    WaitTime = 1
                });
            }
        }

        /// <summary>
        /// 关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Close_Click(object sender, RoutedEventArgs e)
        {
            var res = MessageBox.Show("是否关闭当前窗口",button: MessageBoxButton.YesNo, icon:MessageBoxImage.Question);
            if(res == MessageBoxResult.Yes)
            {
                this.Close();
            }
        }
        /// <summary>
        /// 窗口放大
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Max_Click(object sender, RoutedEventArgs e)
        {
            if (this.WindowState == System.Windows.WindowState.Maximized)
            {
                this.WindowState = System.Windows.WindowState.Normal;
            }
            else
            {
                this.WindowState = System.Windows.WindowState.Maximized;
            }
        }
        /// <summary>
        /// 最小化窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Min_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = System.Windows.WindowState.Minimized;
        }
        /// <summary>
        /// 拖动窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Move_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                this.DragMove();
            }
        }
        private long lastTitleLeftDownTime = 0;
        /// <summary>
        /// 双击最大化或还原,容器控件没有双击数据的无赖之举
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Zoom_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {

            long ticks = DateTime.Now.Ticks / 1000;
            if (ticks - 2000 > lastTitleLeftDownTime)
            {
                lastTitleLeftDownTime = ticks;
            }
            else
            {
                lastTitleLeftDownTime = 0;
                Max_Click(null, null);
            }
        }
    }
}
